/*
 * Created by JFormDesigner on Mon Jun 29 09:48:04 CST 2020
 */

package com.n9.buymsg.view;

import java.awt.event.*;

import com.n9.buymsg.event.ExtractStartEvent;
import com.n9.buymsg.event.MainFormInitEvent;
import com.n9.buymsg.view.table.AnnotationTableModel;
import com.n9.buymsg.view.vo.QqVo;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.awt.*;
import java.util.Iterator;
import java.util.Objects;
import javax.swing.*;

/**
 * @author Brainrain
 */
public class MainForm extends EventComponent {
    private static final MainForm INSTANCE = new MainForm();

    private AnnotationTableModel<QqVo> qqTableModel;

    public static MainForm getInstance() {
        return INSTANCE;
    }

    private MainForm() {
        initComponents();

        initQQTables();

        MainFormInitEvent mainFormInitEvent = new MainFormInitEvent(this);
        mainFormInitEvent.setSynchronizeFlag(false);
        startBtn.setEnabled(false);
        publishEvent(mainFormInitEvent, (args)-> startBtn.setEnabled(true));
    }

    public void addQQVos(QqVo qqVo) {
        qqTableModel.addRow(qqVo);
    }

    /**
     * 更新总消息数
     * @param qqUin
     * @param qqMsgCount
     */
    public void updateQQMsgCount(long qqUin, long qqMsgCount) {
        qqTableModel.getRows().stream().filter(vo -> Objects.equals(qqUin, vo.getUin())).findFirst().ifPresent(vo -> {
            vo.setTotalMsgCount(qqMsgCount);
            updateQqVo(vo);
        });
    }

    /**
     * 更新求购消息数
     * @param qqUin
     * @param qqMsgCount
     */
    public void updateBuyMsgCount(long qqUin, long qqMsgCount) {
        qqTableModel.getRows().stream().filter(vo -> Objects.equals(qqUin, vo.getUin())).findFirst().ifPresent(vo -> {
            vo.setBuyMsgCount(qqMsgCount);
            updateQqVo(vo);
        });
    }


    private void initQQTables() {
        qqTableModel = new AnnotationTableModel<QqVo>(QqVo.class, qqTables){
            @Override
            public void updateRow(QqVo qqVo) {
                Iterator<QqVo> it = rows.iterator();

                int i = 0;
                while(it.hasNext()) {
                    QqVo rowQq = it.next();
                    if (Objects.equals(rowQq.getUin(), qqVo.getUin())) {
                        rowQq.setGroupCount(qqVo.getGroupCount());
                        rowQq.setNick(qqVo.getNick());
                        rowQq.setTotalMsgCount(qqVo.getTotalMsgCount());
                        rowQq.setBuyMsgCount(qqVo.getBuyMsgCount());
                        rowQq.setStatus(qqVo.getStatus());
                        rowQq.setLoginTime(qqVo.getLoginTime());
                        this.fireTableRowsUpdated(i, i);
                        break;
                    }
                    i++;
                }

            }
        };
    }

    /**
     * 显示日志
     * @param string
     */
    public void showLog(String string) {
        SwingUtilities.invokeLater(()-> {
            String msg = String.format("%s:\t%s", DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd hh:MM:ss"), string);
            if (tLog.getLineCount() >= 500) {
                tLog.setText(msg);
            } else {
                tLog.append(msg);
            }
            tLog.append("\r\n");
        });
    }

    public void updateQqVo(QqVo qqVo) {
        qqTableModel.updateRow(qqVo);
    }

    private void button1ActionPerformed(ActionEvent e) {
        startBtn.setEnabled(false);
        ExtractStartEvent extractStartEvent = new ExtractStartEvent(this);
        extractStartEvent.setSynchronizeFlag(false);
        publishEvent(extractStartEvent, (args) -> startBtn.setEnabled(true));
    }

    private void initComponents() {
        this.setSize(1200, 800);
        this.setLocationRelativeTo(null);

        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        tabbedPane1 = new JTabbedPane();
        panel1 = new JPanel();
        panel3 = new JPanel();
        panel6 = new JPanel();
        scrollPane1 = new JScrollPane();
        qqTables = new JTable();
        panel4 = new JPanel();
        panel7 = new JPanel();
        label1 = new JLabel();
        scrollPane2 = new JScrollPane();
        textArea1 = new JTextArea();
        panel11 = new JPanel();
        panel5 = new JPanel();
        panel8 = new JPanel();
        scrollPane3 = new JScrollPane();
        tLog = new JTextArea();
        panel9 = new JPanel();
        startBtn = new JButton();
        panel10 = new JPanel();
        panel2 = new JPanel();

        //======== this ========
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setTitle("\u6c42\u8d2d\u6d88\u606f\u63d0\u53d6\u5668");
        Container contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());

        //======== tabbedPane1 ========
        {

            //======== panel1 ========
            {
                panel1.setPreferredSize(new Dimension(1200, 750));
                panel1.setLayout(new FlowLayout());

                //======== panel3 ========
                {
                    panel3.setPreferredSize(new Dimension(1200, 220));
                    panel3.setLayout(new FlowLayout());

                    //======== panel6 ========
                    {
                        panel6.setPreferredSize(new Dimension(1150, 200));
                        panel6.setLayout(new BorderLayout());

                        //======== scrollPane1 ========
                        {
                            scrollPane1.setViewportView(qqTables);
                        }
                        panel6.add(scrollPane1, BorderLayout.CENTER);
                    }
                    panel3.add(panel6);
                }
                panel1.add(panel3);

                //======== panel4 ========
                {
                    panel4.setPreferredSize(new Dimension(1200, 120));
                    panel4.setLayout(new BorderLayout());

                    //======== panel7 ========
                    {
                        panel7.setPreferredSize(new Dimension(400, 200));
                        panel7.setLayout(new BorderLayout());

                        //---- label1 ----
                        label1.setText("\u6c42\u8d2d\u6d88\u606f\u6807\u8bb0:");
                        panel7.add(label1, BorderLayout.CENTER);

                        //======== scrollPane2 ========
                        {

                            //---- textArea1 ----
                            textArea1.setEditable(false);
                            scrollPane2.setViewportView(textArea1);
                        }
                        panel7.add(scrollPane2, BorderLayout.EAST);
                    }
                    panel4.add(panel7, BorderLayout.CENTER);

                    //======== panel11 ========
                    {
                        panel11.setPreferredSize(new Dimension(25, 10));
                        panel11.setLayout(new BorderLayout());
                    }
                    panel4.add(panel11, BorderLayout.WEST);
                }
                panel1.add(panel4);

                //======== panel5 ========
                {
                    panel5.setPreferredSize(new Dimension(1200, 380));
                    panel5.setLayout(new BorderLayout());

                    //======== panel8 ========
                    {
                        panel8.setPreferredSize(new Dimension(900, 37));
                        panel8.setLayout(new BorderLayout());

                        //======== scrollPane3 ========
                        {
                            scrollPane3.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
                            scrollPane3.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

                            //---- tLog ----
                            tLog.setEditable(false);
                            scrollPane3.setViewportView(tLog);
                        }
                        panel8.add(scrollPane3, BorderLayout.CENTER);
                    }
                    panel5.add(panel8, BorderLayout.CENTER);

                    //======== panel9 ========
                    {
                        panel9.setPreferredSize(new Dimension(250, 48));
                        panel9.setLayout(new FlowLayout());

                        //---- startBtn ----
                        startBtn.setText("\u542f\u52a8");
                        startBtn.addActionListener(e -> button1ActionPerformed(e));
                        panel9.add(startBtn);
                    }
                    panel5.add(panel9, BorderLayout.EAST);

                    //======== panel10 ========
                    {
                        panel10.setPreferredSize(new Dimension(25, 10));
                        panel10.setLayout(new BorderLayout());
                    }
                    panel5.add(panel10, BorderLayout.WEST);
                }
                panel1.add(panel5);
            }
            tabbedPane1.addTab("\u6d88\u606f\u91c7\u96c6", panel1);

            //======== panel2 ========
            {
                panel2.setLayout(new BorderLayout());
            }
            tabbedPane1.addTab("\u914d\u7f6e", panel2);
        }
        contentPane.add(tabbedPane1, BorderLayout.CENTER);
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JTabbedPane tabbedPane1;
    private JPanel panel1;
    private JPanel panel3;
    private JPanel panel6;
    private JScrollPane scrollPane1;
    private JTable qqTables;
    private JPanel panel4;
    private JPanel panel7;
    private JLabel label1;
    private JScrollPane scrollPane2;
    private JTextArea textArea1;
    private JPanel panel11;
    private JPanel panel5;
    private JPanel panel8;
    private JScrollPane scrollPane3;
    private JTextArea tLog;
    private JPanel panel9;
    private JButton startBtn;
    private JPanel panel10;
    private JPanel panel2;

    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
